package rpc_netty.Client.conf;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rpc_netty.server.conf.ConfigureMagment;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class ConfigureMagment_client {

    private volatile static ConfigureMagment_client configureMagment_client;
    private static Logger logger= LoggerFactory.getLogger(ConfigureMagment_client.class);
    private static HashMap<String,String> data=new HashMap<>();

    //默认配置
    static {
        data.put("ServiceRegistryType","Nacos");
        data.put("ServiceRegistryUrl","localhost:8848");
        data.put("ExpireTimes","3000");
        data.put("LoadBalancerPolicy","Random");
        data.put("SerializerType","Json");
    }

    //Singlton
    public static ConfigureMagment_client getConfigureMagment_client() {
        if (configureMagment_client==null){
            synchronized (ConfigureMagment_client.class){
                if (configureMagment_client==null){
                    configureMagment_client=new ConfigureMagment_client();
                }
            }
        }
        return configureMagment_client;
    }

    private ConfigureMagment_client(){
        try {
            FileInputStream fileInputStream=new FileInputStream("src/rpc_client.properties");
            Properties properties=new Properties();
            properties.load(fileInputStream);
            Set<Object> objects = properties.keySet();
            Iterator<Object> iterator = objects.iterator();
            while (iterator.hasNext()){
                String key = (String)iterator.next();
                data.put(key,properties.getProperty(key));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getProperty(String key){
        if (!data.containsKey(key)){
            logger.error("key does not exist");
            return null;
        }else
            return data.get(key);
    }
}
